<?php

/*
 * 	Codificado: 02-Noviembre-2011, 01:50
	Estado: Ajustado_Modelo_Actual(X) Funcionando()
 */


class Suscripciones_Biblioteca_Model extends CI_Model{

	private $table = 'Suscripciones_Biblioteca';

	function _required($required, $data)
	{
		foreach($required as $field)
		if(!isset($data[$field])) return false;

		return true;
	}

	function Insertar($options = array()) {

		/*if ($this->_required(array('IdSuscripcion','AnoCompra','Nombre'), $options))
		return FALSE ;*/

		$this->db->insert($this->table, $options);
		return $this->db->insert_id();

	}

	function Editar($options = array()) {

		if(!$this->_required(array('IdSuscripcion'),$options))
		return FALSE;

		unset($options['submit'] );

		if(isset($options['AnoCompra']))
		$this->db->set('AnoCompra', $options['AnoCompra']);

		if(isset($options['Nombre']))
		$this->db->set('Nombre', $options['Nombre']);

		if(isset($options['MedioDifusion']))
		$this->db->set('MedioDifusion', $options['MedioDifusion']);

		if(isset($options['Cubrimiento']))
		$this->db->set('Cubrimiento', $options['Cubrimiento']);
		
		if(isset($options['ProgramaSuscriptor']))
		$this->db->set('ProgramaSuscriptor', $options['ProgramaSuscriptor']);

			
		$this->db->where('IdSuscripcion', $options['IdSuscripcion']);
		$this->db->update($this->table, $options);

		return $this->db->affected_rows();
	}

	function Eliminar($options = array()) {

		$this->db->where('IdSuscripcion', $options['IdSuscripcion']);
		$this->db->delete($this->table);

		if($data['error'] = $this->db->_error_message());
		return $data;

	}

	function Ver($options = array()) {

		$this->db->where('IdSuscripcion', $options['IdSuscripcion']);
		$query = $this->db->get($this->table);

		if(isset($options['IdSuscripcion']))
		return $query->row_array(0);

		return $query->result_array();
	}

	function Listar($options = array()) {

		$this->db->select('IdSuscripcion,AnoCompra,Nombre' );

		if(isset($options['limit']) && isset($options['offset']))
		$this->db->limit($options['limit'], $options['offset']);
		else if(isset($options['limit']))
		$this->db->limit($options['limit']);
			
		$query = $this->db->get($this->table);
		if(isset($options['count']))
		return $query->num_rows();
			
		return $query->result_array();
	}


	function BuscarPorAno($options = array()) {
		if ($this->_required(array('Ano'), $options)) return FALSE ;

		$this->db->where('Ano', $options['Ano']);
		$query = $this->db->get($this->table);

		return $query->result_array();
	}

}
